/**
 * 核心类型定义
 * 简化版本，专注于内容分发平台的核心需求
 */

/**
 * 环境类型枚举
 */
export enum EnvironmentType {
  /** 浏览器环境 */
  BROWSER = 'browser',
  /** 浏览器扩展环境 */
  EXTENSION = 'extension',
  /** Node.js 环境 */
  NODE = 'node'
}

/**
 * 发布平台枚举
 */
export enum PublishPlatform {
  /** 知乎专栏平台 */
  ZHIHU = 'zhihu',
  /** 今日头条号平台 */
  TOUTIAO = 'toutiao',
  /** 微信公众号 */
  WECHAT_MP = 'wechat-mp',
  /** 小红书平台 */
  XIAOHONGSHU = 'xiaohongshu'
}

/**
 * 日志级别枚举
 */
export enum LogLevel {
  /** 调试级别 */
  DEBUG = 0,
  /** 信息级别 */
  INFO = 1,
  /** 警告级别 */
  WARN = 2,
  /** 错误级别 */
  ERROR = 3
}

/**
 * 内容格式枚举
 */
export enum ContentFormat {
  /** HTML 格式 */
  HTML = 'html',
  /** Markdown 格式 */
  MARKDOWN = 'markdown',
  /** 纯文本 */
  TEXT = 'text'
}

/**
 * 错误代码枚举
 */
export enum ErrorCode {
  // 通用错误
  UNKNOWN_ERROR = 'UNKNOWN_ERROR',
  INITIALIZATION_ERROR = 'INITIALIZATION_ERROR',
  CONFIGURATION_ERROR = 'CONFIGURATION_ERROR',
  VALIDATION_ERROR = 'VALIDATION_ERROR',
  
  // 存储相关错误
  STORAGE_ERROR = 'STORAGE_ERROR',
  FILE_NOT_FOUND = 'FILE_NOT_FOUND',
  FILE_TOO_LARGE = 'FILE_TOO_LARGE',
  STORAGE_QUOTA_EXCEEDED = 'STORAGE_QUOTA_EXCEEDED',
  UPLOAD_FAILED = 'UPLOAD_FAILED',
  
  // 平台相关错误
  PLATFORM_ERROR = 'PLATFORM_ERROR',
  AUTHENTICATION_FAILED = 'AUTHENTICATION_FAILED',
  AUTHORIZATION_FAILED = 'AUTHORIZATION_FAILED',
  PUBLISH_FAILED = 'PUBLISH_FAILED',
  PLATFORM_NOT_SUPPORTED = 'PLATFORM_NOT_SUPPORTED',
  CONTENT_FORMAT_ERROR = 'CONTENT_FORMAT_ERROR',
  IMAGE_PROCESSING_ERROR = 'IMAGE_PROCESSING_ERROR',
  LOGIN_ERROR = 'LOGIN_ERROR',
  
  // 网络相关错误
  NETWORK_ERROR = 'NETWORK_ERROR',
  CONNECTION_TIMEOUT = 'CONNECTION_TIMEOUT',
  REQUEST_FAILED = 'REQUEST_FAILED',
  SERVICE_UNAVAILABLE = 'SERVICE_UNAVAILABLE',
  RATE_LIMIT_EXCEEDED = 'RATE_LIMIT_EXCEEDED',
  
  // API 相关错误
  API_ERROR = 'API_ERROR',
  ACCOUNT_ERROR = 'ACCOUNT_ERROR',
  RATE_LIMIT_ERROR = 'RATE_LIMIT_ERROR',
  
  // 权限相关错误
  PERMISSION_ERROR = 'PERMISSION_ERROR',
  
  // 环境相关错误
  ENVIRONMENT_ERROR = 'ENVIRONMENT_ERROR',
  ENVIRONMENT_NOT_SUPPORTED = 'ENVIRONMENT_NOT_SUPPORTED',
  FEATURE_NOT_AVAILABLE = 'FEATURE_NOT_AVAILABLE',
  
  // 任务相关错误
  TASK_TIMEOUT = 'TASK_TIMEOUT',
  TASK_CANCELLED = 'TASK_CANCELLED',
  NODE_OFFLINE = 'NODE_OFFLINE',
  CLUSTER_ERROR = 'CLUSTER_ERROR'
}

/**
 * 错误详情接口
 */
export interface IErrorDetails {
  code: ErrorCode;
  message: string;
  details?: any;
  timestamp?: Date;
  stack?: string;
}

/**
 * 日志条目
 */
export interface ILogEntry {
  level: LogLevel;
  message: string;
  timestamp: Date;
  source?: string;
  context?: Record<string, any>;
  error?: Error;
  userId?: string;
}

/**
 * 基础配置接口
 */
export interface IConfig {
  environment?: EnvironmentType;
  debug?: boolean;
  logLevel?: LogLevel;
  timeout?: number;
  retry?: {
    maxRetries: number;
    retryDelay: number;
  };
}

/**
 * API 响应接口
 */
export interface IApiResponse<T = any> {
  success: boolean;
  data?: T;
  error?: IErrorDetails;
  message?: string;
  timestamp: Date;
}

/**
 * 可释放资源接口
 */
export interface IDisposable {
  readonly disposed: boolean;
  dispose(): void | Promise<void>;
}

/**
 * 可初始化接口
 */
export interface IInitializable {
  readonly initialized: boolean;
  initialize(config?: any): Promise<void>;
}

/**
 * 默认常量
 */
export const DEFAULT_CONFIG = {
  /** 默认超时时间（毫秒） */
  DEFAULT_TIMEOUT: 30 * 1000,
  /** 默认最大重试次数 */
  DEFAULT_MAX_RETRIES: 3,
  /** 默认重试延迟 */
  DEFAULT_RETRY_DELAY: 1000,
  /** 默认文件大小限制（10MB） */
  DEFAULT_MAX_FILE_SIZE: 10 * 1024 * 1024,
  /** 支持的图片格式列表 */
  SUPPORTED_IMAGE_FORMATS: [
    'image/jpeg',
    'image/png',
    'image/gif',
    'image/webp'
  ] as const
} as const;

/**
 * 工具类型定义
 */
export type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

export type Nullable<T> = T | null;

export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
